package cn.tedu.erp.project.controller;

import cn.tedu.erp.project.ex.ServiceException;
import cn.tedu.erp.project.pojo.dto.DeptAddNewDTO;
import cn.tedu.erp.project.pojo.dto.DeptUpdateDTO;
import cn.tedu.erp.project.pojo.vo.AdminListVO;
import cn.tedu.erp.project.pojo.vo.AdminStandardVO;
import cn.tedu.erp.project.pojo.vo.DeptListVO;
import cn.tedu.erp.project.pojo.vo.DeptStandardVO;
import cn.tedu.erp.project.service.IDeptService;
import cn.tedu.erp.project.web.JsonResult;
import cn.tedu.erp.project.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
@Api(tags = "01.部门管理模块")
public class DeptController {

    public DeptController() {
        log.debug("创建控制器对象:DeptController");
    }

    @Autowired
    IDeptService service;

    //http://localhost:9090/depts/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加部门")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/erp_pwdb/dept/add-new')")
    public JsonResult<Void> addNew(DeptAddNewDTO deptAddNewDTO) {
        log.debug("开始处理[添加部门]的请求,请求参数:{}", deptAddNewDTO);
        service.addNew(deptAddNewDTO);
        return JsonResult.ok();
    }

    //http://localhost:9090/depts/0-9/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除部门")
    @PreAuthorize("hasAuthority('/erp_pwdb/dept/delete')")//检查用户是否有权限
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "部门ID", required = true, dataType = "long")
    )
    public JsonResult<Void> delete(@PathVariable @Range(min = 2, message = "请提交有效的ID值!") Long id) {
        log.debug("开始处理[删除部门]的请求,请求参数:{}", id);
        service.deleteById(id);
        return JsonResult.ok();
    }

    //http://localhost:9090/depts/0-9/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改部门")
    @PreAuthorize("hasAuthority('/erp_pwdb/dept/update')")//检查用户是否有权限
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "部门ID", required = true, dataType = "long")
    )
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 2, message = "请提交有效的ID值!") Long id,
                                           @Valid DeptUpdateDTO deptUpdateDTO) {
        log.debug("开始处理修改相册的请求,参数为id={},修改后的数据={}", id, deptUpdateDTO);
        service.updateById(id, deptUpdateDTO);
        return JsonResult.ok();
    }


    //http://localhost:9090/depts/list
    @GetMapping("")
    @ApiOperation("查询部门列表")
    @PreAuthorize("hasAuthority('/erp_pwdb/dept/read')")//检查用户是否有权限
    @ApiOperationSupport(order = 410)
    public JsonResult<List<DeptListVO>> list() {
        log.debug("开始处理[查询部门]的请求,无参数");
        List<DeptListVO> list = service.list();
        return JsonResult.ok(list);
    }

    //http://localhost:9090/depts/959595/list-admin
    @PostMapping("/{id:[0-9]+}/list-admin")
    @ApiOperation("查询部门用户列表")
    @PreAuthorize("hasAuthority('/erp_pwdb/dept/read')")//检查用户是否有权限
    @ApiOperationSupport(order = 420)
    public JsonResult<List<AdminListVO>> listAdmin(@PathVariable @Range(min = 2, message = "请提交有效的ID值!") Long id) {
        log.debug("开始处理[查询部门详情]的请求,参数为：{}", id);
        List<AdminListVO> list = service.listAdmin(id);
        return JsonResult.ok(list);
    }

    //http://localhost:9090/depts/getbyname
    @ApiOperation("根据部门名称查询部门详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{name}")
    @PreAuthorize("hasAuthority('/erp_pwdb/dept/read')")//检查用户是否有权限
    public JsonResult<DeptStandardVO> getStandardByName(@PathVariable("name") String name) {
        log.debug("开始处理查询用户详情的请求,参数为nickname={}", name);
        DeptStandardVO standardVO = service.getStandardByName(name);
        log.debug("查询到的结果为,{}", standardVO);
        return JsonResult.ok(standardVO);
    }
}
